package com.newworld.dmsapp2;

import io.yunba.android.manager.YunBaManager;

import java.util.Random;

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.newworld.dmsapp2.activity.MainActivity;
import com.newworld.dmsapp2.bean.User;
import com.newworld.dmsapp2.util.SingleSample;


public class DemoReceiver extends BroadcastReceiver {

	private final static String REPORT_MSG_SHOW_NOTIFICARION = "1000";
	private final static String REPORT_MSG_SHOW_NOTIFICARION_FAILED = "1001";
	
	
	/*这里notification_id最好是云吧传的信息里的repiar_id;*/
	static Random random = new Random();
	public static int notification_id = random.nextInt();
	public static int count = 0;
	//int notification_id = 19172439;
	NotificationManager nm;
	@SuppressWarnings("static-access")
	@Override
	public void onReceive(final Context context, Intent intent) {
		User user;
		user = SingleSample.getInstance(context).getUser();
		 if (YunBaManager.MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {

		        String topic = intent.getStringExtra(YunBaManager.MQTT_TOPIC);
		        String msg = intent.getStringExtra(YunBaManager.MQTT_MSG);
		        
		        System.out.println("云吧来的msg是："+msg+""+"你订阅的Topic是："+topic);
		        //在这里处理从服务器发布下来的消息， 比如显示通知栏， 打开 Activity 等等
		        StringBuilder showMsg = new StringBuilder();
		        showMsg.append("Received message from server: ")
		                .append(YunBaManager.MQTT_TOPIC)
		                .append(" = ")
		                .append(topic)
		                .append(" ")
		                .append(YunBaManager.MQTT_MSG)
		                .append(" = ")
		                .append(msg);
		        DemoUtil.showNotifation(context, topic, msg);
		        Intent intent1 = new Intent("com.dwtedx.alarmmanagerdemo.MyReceiver");
	      		intent1.putExtra("msg", "你该打酱油了");
	      		//只有服务人员才出现这种提示。
	      		if(DemoUtil.flagForServer&&count>0){
	      		// 创建PendingIntent对象封装Intent，由于是使用广播，注意使用getBroadcast方法
		      		PendingIntent pi= PendingIntent.getBroadcast(context, 0, new Intent(context,MyReceiver.class), 0);
		      		// 获取AlarmManager对象
		      		AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
		      		am.cancel(pi);  
		      		// 设置闹钟从当前时间开始，每隔10分钟执行一次PendingIntent对象，注意第一个参数与第二个参数的关系
		      		am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10 * 1000, pi);
			        System.out.println("本消息是showMsg11：");
			        
			        //count++;
	      		}
		    } else if(YunBaManager.PRESENCE_RECEIVED_ACTION.equals(intent.getAction())) {
			 //msg from presence.
			 String topic = intent.getStringExtra(YunBaManager.MQTT_TOPIC);		
				String payload = intent.getStringExtra(YunBaManager.MQTT_MSG);
				StringBuilder showMsg = new StringBuilder();
				showMsg.append("Received message presence: ")
						.append(YunBaManager.MQTT_TOPIC)
						.append(" = ")
						.append(topic)
						.append(" ")
						.append(YunBaManager.MQTT_MSG)
						.append(" = ")
						.append(payload);
				DemoUtil.showNotifation(context, topic, payload);
				boolean flag = DemoUtil.showNotifation(context, topic, payload);
				System.out.println("本消息是showMsg22");
				Log.d("DemoReceiver", showMsg.toString());
				 //上报显示通知栏状态， 以方便后台统计
			     if (flag) YunBaManager.report(context, REPORT_MSG_SHOW_NOTIFICARION, topic);
			     else  YunBaManager.report(context, REPORT_MSG_SHOW_NOTIFICARION_FAILED, topic);
		}
	}
	//send msg to MainActivity
	@SuppressWarnings("unused")
	private void processCustomMessage(Context context, Intent intent) {
	
			intent.setAction(MainActivity.MESSAGE_RECEIVED_ACTION);
			intent.addCategory(context.getPackageName());
			context.sendBroadcast(intent);
		
	}
	
}
